#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
    // 检查图片路径是否已作为参数传递
    if (argc < 2) {
        cerr << "Usage: DisplayImage \"/home/gavgv/060606.jpeg\"" << endl;
        return -1;
    }

    Mat image;
    image = imread(argv[1], IMREAD_COLOR); // Read the file

    if(!image.data) {
        cerr <<  "Could not open or find the image" << endl ;
        return -1;
    }

    // 分离颜色通道
    vector<Mat> bgr_channels;
    split(image, bgr_channels);
    Mat blueChannel = bgr_channels[0];
    Mat greenChannel = bgr_channels[1];
    Mat redChannel = bgr_channels[2];

    // 显示分离的通道
    namedWindow("Blue Channel", WINDOW_AUTOSIZE);
    imshow("Blue Channel", blueChannel);
    namedWindow("Green Channel", WINDOW_AUTOSIZE);
    imshow("Green Channel", greenChannel);
    namedWindow("Red Channel", WINDOW_AUTOSIZE);
    imshow("Red Channel", redChannel);

    // 等待用户按键
    waitKey(0);

    // 转换成BCHW格式
    int nChannels = 3;
    int dims[] = {1, nChannels, image.rows, image.cols};
    Mat bchwMat;
    merge(bgr_channels, bchwMat); // 合并通道，保持BGR顺序
    permute(bchwMat, bchwMat, cv::PERMUTE(0, 2, 3, 1)); // 转换为BCHW格式

    // 注意：这里没有直接显示BCHW格式的图像，因为它是一个多维数组，不是标准的2D图像
    // BCHW格式通常用于深度学习框架中

    return 0;
}